package net.joint_painting.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author CAM
 * @version $Revision: 1.0 $
 */
public class Window extends Composite implements ResizeHandler {

    /**
     * @author CAM
     * @version $Revision: 1.0 $
     */
    interface WindowUiBinder extends UiBinder<Widget, Window> {
    }

    private static WindowUiBinder uiBinder = GWT.create(WindowUiBinder.class);
    @UiField
    SimplePanel NavigationPanel;
    @UiField
    SimplePanel SpacePanel;
    @UiField
    AbsolutePanel absolutePanel;
    @UiField
    SimplePanel ColorPickerPanel;

    @UiField
    SimplePanel PainterPickerPanel;

    String id;
    net.joint_painting.client.model.Model model;
    net.joint_painting.client.view.Navigation navigation;
    net.joint_painting.client.view.Space space;
    net.joint_painting.client.view.ColorPicker colorPicker;
    net.joint_painting.client.view.PainterPicker painterPicker;

    /**
     * Constructor for Window.
     * 
     * @param id
     *            String
     */
    public Window(String id) {
	this.id = id;
	initWidget(uiBinder.createAndBindUi(this));
	com.google.gwt.user.client.Window.addResizeHandler(this);
	init();
    }

    private void init() {
	try {
	    model = new net.joint_painting.client.model.Model(id);
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	navigation = new net.joint_painting.client.view.Navigation();
	space = new net.joint_painting.client.view.Space();
	colorPicker = new net.joint_painting.client.view.ColorPicker();
	painterPicker = new net.joint_painting.client.view.PainterPicker();

	initView();
	onResize(null);
    }

    private void initView() {
	space.addViewObserver(model.getViewController()
		.getViewObserverListener());
	navigation.addViewObserver(model.getViewController()
		.getViewObserverListener());
	colorPicker.addViewObserver(model.getViewController()
		.getViewObserverListener());
	painterPicker.addViewObserver(model.getViewController()
		.getViewObserverListener());
	space.setModel(model);
	NavigationPanel.add(navigation);
	SpacePanel.add(space);
	ColorPickerPanel.add(colorPicker);
	PainterPickerPanel.add(painterPicker);
    }

    /**
     * Method onResize.
     * 
     * @param event
     *            ResizeEvent
     * 
     * @see com.google.gwt.event.logical.shared.ResizeHandler#onResize(ResizeEvent)
     */
    @Override
    public void onResize(ResizeEvent event) {
	Integer width = com.google.gwt.user.client.Window.getClientWidth() - 20;
	Integer height = com.google.gwt.user.client.Window.getClientHeight() - 20;
	this.setSize(width + "px", height + "px");
	absolutePanel.setSize(width + "px", height + "px");
	SpacePanel.setSize(width + "px", height + "px");
	space.setSize(width, height);
    }
}
